package web.reclamos.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import web.reclamos.beans.Area;
import web.reclamos.beans.Coorporacion;
import web.reclamos.beans.Tipo;
import web.reclamos.service.AreaService;
import web.reclamos.service.CoorporacionService;
import web.reclamos.service.TipoService;

public class CoorporacionServlet extends HttpServlet {
   

    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        if(request.getServletPath().equals("/coorporacion_nuevo.reclamo")) {
            nuevoCoorporacion(request, response);
        }else if(request.getServletPath().equals("/coorporacion_modificar.reclamo")) {
            modificarProducto(request, response);
        }else if(request.getServletPath().equals("/coorporacion_eliminar.reclamo")) {
            eliminarProducto(request, response);
        }else if(request.getServletPath().equals("/tipo_listar.reclamo")) {
            listarTipos(request, response);
        }else if(request.getServletPath().equals("/coorporacion_consultar.reclamo")) {
            consultarCoorporacion(request, response);
        }
    }

    private void nuevoCoorporacion(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {            
          CoorporacionService cooService = new CoorporacionService();
          Coorporacion coorporacion = new Coorporacion();
            System.out.println("+++++++++++++++++++++++++++");
            System.out.println(request.getParameter("txtId"));
            System.out.println(request.getParameter("txtNombre"));
            System.out.println(request.getParameter("txtDireccion"));
            System.out.println(request.getParameter("txtRepresentante"));
            System.out.println("+++++++++++++++++++++++++++");
          coorporacion.setId_coorporacion(Integer.parseInt(request.getParameter("txtId")));
          coorporacion.setNombre_coorporacion(request.getParameter("txtNombre"));
          coorporacion.setDireccion_coorporacion(request.getParameter("txtDireccion"));
          coorporacion.setRepresentante_legal(request.getParameter("txtRepresentante"));

           try {
                cooService.registrarCoorporacion(coorporacion);
            }catch(Exception ex) {
                //Ir pagina de error
            }
           //Regresar pagina producto_nuevo
          response.sendRedirect("TablasExito.jsp");
    }
    private void modificarProducto(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        /*ProductoService proService = new ProductoService();
        String filtro = request.getParameter("txtFiltro");

        Producto producto = new Producto();
        producto.setId(Integer.parseInt(request.getParameter("txtId")));
        producto.setNombre(request.getParameter("txtNombre"));
        producto.setPrecio(Float.parseFloat(request.getParameter("txtPrecio")));
        producto.setStock(Integer.parseInt(request.getParameter("txtStock")));

        proService.modificarProducto(producto);

        String url = "/producto_consultar.isil?txtNombre=" + filtro ;
        getServletContext().getRequestDispatcher(url).forward(request, response);*/
    }

    private void eliminarProducto(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        /*ProductoService proService = new ProductoService();
        int id = Integer.parseInt(request.getParameter("id"));

        proService.eliminarProducto(id);

        String url = "/producto_consultar.isil?txtNombre=" + request.getParameter("txtNombre") ;
        getServletContext().getRequestDispatcher(url).forward(request, response);*/
    }

    private void listarTipos(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        TipoService tipService = new TipoService();
        CoorporacionService cooService = new CoorporacionService();
        AreaService areService = new AreaService();
        
       List<Tipo> tipos = tipService.devolverTodos();
       List<Coorporacion> coorporaciones = cooService.devolverTodos();
       List<Area> areas = areService.devolverTodos();

       request.setAttribute("tipos", tipos);
       request.setAttribute("coorporaciones", coorporaciones);
       request.setAttribute("areas", areas);

        String url = "/ReclamoCrear.jsp";
        getServletContext().getRequestDispatcher(url).forward(request, response);
    }

    private void consultarCoorporacion(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        CoorporacionService cooService = new CoorporacionService();
        String nombre = request.getParameter("txtNombre");

        List<Coorporacion> coorporaciones = cooService.consultarPorNombre(nombre);

        request.setAttribute("nombre", nombre);
        request.setAttribute("coorporaciones", coorporaciones);

        String url = "/TablasCoorporacion.jsp";
        getServletContext().getRequestDispatcher(url).forward(request, response);

    }
}
